//
//  subViewForSearchPartsInventoryView.m
//  HondaPro_2
//
//  Created by Eric on 13-6-19.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "subViewForSearchPartsInventoryView.h"
#import "UserDefinedCellForSubViewOfSearchPartsInventory.h"
#import "MainMenuDataModel.h"

@interface subViewForSearchPartsInventoryView ()
@property(nonatomic,strong)NSDictionary * getDic;

//创建控件
-(void)createStaticLabels;
-(void)createTableView;

//刷新控件内容
-(void)freshenViewDis;

@end

@implementation subViewForSearchPartsInventoryView

@synthesize tableViewDis = _tableViewDis;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        
        [self createTableView];
        [self createStaticLabels];
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    
    [self freshenViewDis];
}

#pragma mark - 创建控件

-(void)createStaticLabels
{
    NSArray *arrForAllKeys = [NSArray arrayWithObjects:
                              @"bottomLbl",
                              @"PART_CODE",
                              @"PART_CHINA_NAME",
                              @"PART_ENGLISH_NAME",
                              @"CAR_TYPE_ID",
                              @"UNITPRICE",
                              @"PART_STORAGE_QTY",
                              @"AVAILBLE_QTY", nil];
    
    NSDictionary *dicForStaticLabelsText = [NSDictionary dictionaryWithObjectsAndKeys:
                                        @"",@"bottomLbl",
                                        @"零件编号",@"PART_CODE",
                                        @"零件中文名",@"PART_CHINA_NAME",
                                        @"拼音代码",@"PART_ENGLISH_NAME",
                                        @"零件类别",@"CAR_TYPE_ID",
                                        @"单价",@"UNITPRICE",
                                        @"库存数",@"PART_STORAGE_QTY",
                                        @"可用数",@"AVAILBLE_QTY", nil];
    
    NSDictionary *dicForStaticLabelsRect = [NSDictionary dictionaryWithObjectsAndKeys:
                                            [NSValue valueWithCGRect:CGRectMake(0, 0, self.frame.size.width, 50)],@"bottomLbl",
                                            [NSValue valueWithCGRect:CGRectMake(0, 0, 134, 50)],@"PART_CODE",
                                            [NSValue valueWithCGRect:CGRectMake(135, 0, 134, 50)],@"PART_CHINA_NAME",
                                            [NSValue valueWithCGRect:CGRectMake(270, 0, 134, 50)],@"PART_ENGLISH_NAME",
                                            [NSValue valueWithCGRect:CGRectMake(405, 0, 134, 50)],@"CAR_TYPE_ID",
                                            [NSValue valueWithCGRect:CGRectMake(540, 0, 134, 50)],@"UNITPRICE",
                                            [NSValue valueWithCGRect:CGRectMake(675, 0, 134, 50)],@"PART_STORAGE_QTY",
                                            [NSValue valueWithCGRect:CGRectMake(810, 0, 128, 50)],@"AVAILBLE_QTY",nil];
    
    for (int i = 0; i<[arrForAllKeys count]; i++)
    {
        UILabel *lbl = [[UILabel alloc] initWithFrame:[[dicForStaticLabelsRect objectForKey:[arrForAllKeys objectAtIndex:i]] CGRectValue]];
        if ([[arrForAllKeys objectAtIndex:i] isEqualToString:@"bottomLbl"])
        {
            lbl.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
        }
        else
        {
            lbl.backgroundColor = [UIColor clearColor];
        }
        lbl.font = [UIFont systemFontOfSize:15];
        lbl.text = [dicForStaticLabelsText objectForKey:[arrForAllKeys objectAtIndex:i]];
        lbl.textColor = [UIColor grayColor];
        [self addSubview:lbl];        
    }
}

-(void)createTableView
{
    self.tableViewDis = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.frame.size.width, self.frame.size.height-50)];
    
//    PullRefreshTableViewController *xxTmp = [[PullRefreshTableViewController alloc] initWithStyle:UITableViewStylePlain];
//    self.tableViewDis = xxTmp.tableView;
//    self.tableViewDis.frame = CGRectMake(0, 50, self.frame.size.width, self.frame.size.height-50);
    
    self.tableViewDis.dataSource = self;
    self.tableViewDis.delegate = self;
    
    self.tableViewDis.backgroundColor = [UIColor clearColor];
    
    [self addSubview:self.tableViewDis];
}

//tableView的ScrollView代理方法
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate //判断上拉的偏移量
{
//    if (detailedtabelView.contentOffset.y > _imageArray.count * 80 - detailedtabelView.bounds.size.height + 80)
//    {
//        NSLog(@"%f", detailedtabelView.contentOffset.y);
//        [self asynGetRequest];
//    }
    
    //发送请求数据的消息
//    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
//    NSArray *arr = modelIns.arrForSearchPartsInventory;
    
//    if (self.tableViewDis.contentOffset.y > arr.count * 50-)
//    {
//        NSLog(@"hahahah!!!");
//    }
    
    //上拉刷新
    if(scrollView.contentSize.height - (scrollView.contentOffset.y + scrollView.bounds.size.height - scrollView.contentInset.bottom) <= -52.0f && scrollView.contentOffset.y > 0)
    {
        //给父视图发送一个消息
        [[NSNotificationCenter defaultCenter] postNotificationName:@"searchPartInventoryToUpdateTheAsyPartsData" object:nil];
    }
}

#pragma mark - 刷新控件内容

-(void)freshenViewDis
{
    [self.tableViewDis reloadData];
}

#pragma mark - tableView DataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    NSArray *arr = modelIns.arrForSearchPartsInventory;
    return [arr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    UserDefinedCellForSubViewOfSearchPartsInventory *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (nil == cell)
    {
        cell = [[UserDefinedCellForSubViewOfSearchPartsInventory alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    NSInteger row = [indexPath row];
    
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    NSArray *arr = modelIns.arrForSearchPartsInventory;
    
    //cell的风格是：浅浅灰色和浅灰色间隔出现，第一行是浅灰色
    switch (row%2)
    {
        case 0:
            //浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
            break;
        case 1:
            //浅浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1];
            break;
            
        default:
            break;
    }
    
    if (row<[arr count])
    {
        NSDictionary *dicTmp = [arr objectAtIndex:row];
        self.getDic = dicTmp;
        NSLog(@"getDic---------%@",dicTmp);
        for (int i = 0; i<[cell.arrForAllKeys count]; i++)
        {
            id obj = [dicTmp objectForKey:[cell.arrForAllKeys objectAtIndex:i]];
            if ([obj isKindOfClass:[NSString class]]) {
                ((UILabel *)[cell.dicForAllLabels objectForKey:[cell.arrForAllKeys objectAtIndex:i]]).text = (NSString *)obj;
            }
            else if ([obj isKindOfClass:[NSNumber class]]){
                NSNumber *number = (NSNumber *)obj;
                ((UILabel *)[cell.dicForAllLabels objectForKey:[cell.arrForAllKeys objectAtIndex:i]]).text = number.stringValue;
            }
            if (i == 3) {
//                NSNumber *number = (NSNumber *)obj;

              ((UILabel *)[cell.dicForAllLabels objectForKey:[cell.arrForAllKeys objectAtIndex:i]]).text = [self getlingjianleixing];
            }
            
        }
    }
    else
    {
        for (int i = 0; i<[cell.arrForAllKeys count]; i++)
        {
            ((UILabel *)[cell.dicForAllLabels objectForKey:[cell.arrForAllKeys objectAtIndex:i]]).text = @"";
        }
    }
    
    return cell;
}

#pragma mark - tableView Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
-(NSString *)getlingjianleixing{
    NSArray *arr = [self readershuDICimform][@"TM_PART_TYPE"];
    for (NSDictionary *dic in arr) {
        
        NSString *code1 = dic[@"CODE"];
        NSString *code2 = [self.getDic[@"CAR_TYPE_ID"] stringValue];
        if ([code1 isEqualToString:code2]) {
            return dic[@"NAME"];
        }
    }
    
    return nil;
    
}
- (NSMutableDictionary *)readershuDICimform
{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path=[paths objectAtIndex:0];
    NSString *str=[path stringByAppendingPathComponent:@"Dic.text"];
    dic=[NSMutableDictionary dictionaryWithContentsOfFile:str];
    //nslog(@"%@",dic);
    return dic;
}
@end
